//
//  FQFeedbackViewController.swift
//  SwiftProject
//
//  Created by 董駸 on 2022/8/24.
//

import UIKit

class FQFeedbackViewController: BaseScrollViewController {
    let textView = QMUITextView()
    let imageBtn = QMUIButton()
    var btnArray1 : [QMUIButton] = []
    var btnArray2 : [QMUIButton] = []

    override func viewDidLoad() {
        super.viewDidLoad()
        navigationBarType = .light
    }
    
    override func initSubviews() {
        super.initSubviews()
        
        let titleLb = QMUILabel(font: .rSFont(14), textColor: .color102)
        titleLb.text = "请留下您宝贵的意见与建议，我们将在3个工作日内给您反馈"
        titleLb.numberOfLines = 0
        scrollContentView.addSubview(titleLb)
        
        titleLb.snp.makeConstraints { make in
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.top.equalTo(18)
        }
        
        let whiteV1 = UIView()
        whiteV1.backgroundColor = .white
        whiteV1.layer.cornerRadius = 8
        whiteV1.clipsToBounds = true
        scrollContentView.addSubview(whiteV1)
        
        whiteV1.snp.makeConstraints { make in
            make.left.right.equalTo(titleLb)
            make.top.equalTo(titleLb.snp.bottom).offset(16)
            make.height.equalTo(214)
        }
        
        textView.placeholder = "请输入您想要提问的问题(需填写产生问题的产品)"
        textView.font = .rSFont(16)
        textView.placeholderColor = .color206
        textView.textColor = .color51
        whiteV1.addSubview(textView)
        
        textView.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(16)
            make.height.equalTo(120)
        }
        
        imageBtn.setBackgroundImage(UIImage(named: "icon_updateImage"), for: .normal)
        imageBtn.addTarget(self, action: #selector(imageTap(_:)), for: .touchUpInside)
        whiteV1.addSubview(imageBtn)
        
        imageBtn.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.size.equalTo(66)
            make.bottom.equalTo(-16)
        }
        
        let whiteV2 = UIView()
        whiteV2.backgroundColor = .white
        whiteV2.layer.cornerRadius = 8
        whiteV2.clipsToBounds = true
        scrollContentView.addSubview(whiteV2)
        
        whiteV2.snp.makeConstraints { make in
            make.left.right.equalTo(titleLb)
            make.top.equalTo(whiteV1.snp.bottom).offset(16)
            make.height.equalTo(138)
        }
        
        let titleLb1 = QMUILabel(font: .rSFont(16), textColor: .color51)
        titleLb1.text = "请选择问题类型"
        whiteV2.addSubview(titleLb1)
        
        titleLb1.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(16)
        }
        
        let floatView1 = QMUIFloatLayoutView()
        floatView1.itemMargins = UIEdgeInsets(top: 12, left: 8, bottom: 0, right: 8)
        floatView1.minimumItemSize = CGSize(width: 80, height: 28)
        whiteV2.addSubview(floatView1)
        
        floatView1.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(titleLb1.snp.bottom).offset(16)
            make.bottom.equalTo(-5)
        }
        
        let strArray1 = ["还款问题", "申请问题", "会员问题", "违规举报", "其他问题"]
        
        for str in strArray1 {
            let btn = QMUIButton(title: str, titleColor: .color206, font: .rSFont(14))
            btn.setTitleColor(.white, for: .selected)
            btn.clipsToBounds = true
            btn.setBackgroundWithColor(.white, for: .normal)
            btn.setBackgroundWithColor(.colorMain, for: .selected)
            btn.layer.cornerRadius = 14
            btn.clipsToBounds = true
            btn.layer.borderColor = UIColor.color206.cgColor
            btn.layer.borderWidth = 1
            btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
            btn.addTarget(self, action: #selector(buttonClick1(_:)), for: .touchUpInside)
            floatView1.addSubview(btn)
            btnArray1.append(btn)
        }
        
        let whiteV3 = UIView()
        whiteV3.backgroundColor = .white
        whiteV3.layer.cornerRadius = 8
        whiteV3.clipsToBounds = true
        scrollContentView.addSubview(whiteV3)
        
        whiteV3.snp.makeConstraints { make in
            make.left.right.equalTo(titleLb)
            make.top.equalTo(whiteV2.snp.bottom).offset(16)
            make.height.equalTo(138)
        }
        
        let titleLb2 = QMUILabel(font: .rSFont(16), textColor: .color51)
        titleLb2.text = "请选择问题的机构"
        whiteV3.addSubview(titleLb2)
        
        titleLb2.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.top.equalTo(16)
        }
        
        let floatView2 = QMUIFloatLayoutView()
        floatView2.itemMargins = UIEdgeInsets(top: 12, left: 8, bottom: 0, right: 8)
        floatView2.minimumItemSize = CGSize(width: 80, height: 28)
        whiteV3.addSubview(floatView2)
        
        floatView2.snp.makeConstraints { make in
            make.left.equalTo(16)
            make.right.equalTo(-16)
            make.top.equalTo(titleLb2.snp.bottom).offset(16)
            make.bottom.equalTo(-5)
        }
        
        let strArray2 = ["小象钱包", "有钱花", "召集令", "普融花-恒易贷", "携程借款", "悦借"]
        
        for str in strArray2 {
            let btn = QMUIButton(title: str, titleColor: .color206, font: .rSFont(14))
            btn.setTitleColor(.white, for: .selected)
            btn.clipsToBounds = true
            btn.setBackgroundWithColor(.white, for: .normal)
            btn.setBackgroundWithColor(.colorMain, for: .selected)
            btn.layer.cornerRadius = 14
            btn.clipsToBounds = true
            btn.layer.borderColor = UIColor.color206.cgColor
            btn.layer.borderWidth = 1
            btn.contentEdgeInsets = UIEdgeInsets(top: 0, left: 12, bottom: 0, right: 12)
            btn.addTarget(self, action: #selector(buttonClick2(_:)), for: .touchUpInside)
            floatView2.addSubview(btn)
            btnArray2.append(btn)
        }

        let sendBtn = QMUIFillButton(fill: .colorMain, titleTextColor: .white)
        sendBtn.setTitle("提交", for: .normal)
        sendBtn.titleLabel?.font = .mSFont(16)
        sendBtn.addTarget(self, action: #selector(send), for: .touchUpInside)
        scrollContentView.addSubview(sendBtn)
        
        sendBtn.snp.makeConstraints { make in
            make.left.equalTo(48)
            make.right.equalTo(-48)
            make.height.equalTo(42)
            make.top.equalTo(whiteV3.snp.bottom).offset(11)
        }
    }
    
    @objc func send() {
        if textView.text.isEmpty {
            QMUITips.showError(textView.placeholder)
            return
        }
        
    }
    
    @objc func imageTap(_ button : QMUIButton) {
        let actions: [QMUIAlertController.AlertAction] = [
            .action(title: "拍照"),
            .action(title: "从手机相册选择"),
            .action(title: "取消", style : .cancel)
        ]

        QMUIAlertController.present(title: nil, message: nil, style: .actionSheet, actions: actions).subscribe(onNext: { buttonIndex in
            PromiseManager.shared.getCameraPermissionsCompletion { [unowned self] isOn in
                UIImagePickerController.rx.createWithParent(self) { picker in
                    picker.sourceType = buttonIndex == 0 ? .camera : .photoLibrary
                    picker.allowsEditing = true
                }.flatMap { $0.rx.didFinishPickingMediaWithInfo }.take(1).subscribe(onNext: { dict in
                    let image = dict[UIImagePickerController.InfoKey.editedImage] as! UIImage
                    button.setBackgroundImage(image, for: .normal)
                }).disposed(by: disposeBag)
            }
        }).disposed(by: disposeBag)
    }
    
    @objc func buttonClick1(_ button : QMUIButton) {
        for button in btnArray1 {
            button.isSelected = false
            button.layer.borderWidth = 1
        }
        button.isSelected = true
        button.layer.borderWidth = 0
    }

    @objc func buttonClick2(_ button : QMUIButton) {
        for button in btnArray2 {
            button.isSelected = false
            button.layer.borderWidth = 1
        }
        button.isSelected = true
        button.layer.borderWidth = 0
    }
    
    override func setupNavigationItems() {
        super.setupNavigationItems()
        title = "问题反馈"
    }
}
